package cn.com.sinux.spring.commons;

import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2016/2/19.
 */
public class PropertyCollection {

    private Set<String> properties;
    private Set<String> containProperties;

    public Set<String> getContainProperties() {
        return containProperties;
    }

    public void setContainProperties(Set<String> containProperties) {
        this.containProperties = containProperties;
    }

    public Set<String> getProperties() {
        return properties;
    }

    public PropertyCollection() {
        this.properties = new HashSet<String>();
        this.containProperties = new HashSet<String>();
    }

    public PropertyCollection(String... properties) {
        this.properties = new HashSet<String>();
        this.properties.addAll(Arrays.asList(properties));
    }

    public void setProperties(Set<String> properties) {
        this.properties = properties;
    }

    public PropertyCollection removeProperties(String... properties) {
        List<String> pp = Arrays.asList(properties);
        this.properties.addAll(pp);
        this.containProperties.removeAll(pp);
        return this;
    }

    public PropertyCollection addProperties(String... properties) {
        List<String> pp = Arrays.asList(properties);
        this.containProperties.addAll(pp);
        this.properties.removeAll(this.containProperties);
        return this;
    }

    public PropertyCollection addProperties(Class clasze, String... properties) {
        Set<String> pro = new HashSet<String>();
        PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(clasze);
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            pro.add(propertyDescriptor.getName());
        }
        List<String> pp = Arrays.asList(properties);
        this.properties.addAll(pro);
        this.containProperties.addAll(pp);
        this.properties.removeAll(this.containProperties);
        return this;
    }

    public PropertyCollection addPropertyCollection(PropertyCollection propertyCollection) {
        this.properties.addAll(propertyCollection.getProperties());
        return this;
    }
}
